
local function _fact(n)
    if n <= 1 then
        return 1
    else
        return n * _fact(n-1)
    end
end

-- tail recursion
local function _fact_tail(n, v)
    if n <= 1 then
        return v
    else
        return _fact_tail(n-1, n*v)
    end
end

local function test()
    local val = 5
    local n1 = 1
    for i=1, val do
        n1 = n1 * i
    end

    local n2 = _fact(val)
    local n3 = _fact_tail(val, 1)
    assert(n1 == n2)
    assert(n2 == n3)
end

test()

